/** @file button.cpp
* @brief: Button
*/
#include "frgui.h"
#include "widget.h"
#include "button.h"
#include "string.h"
#include "stdio.h"

#include "message.h"
#include "gdi.h"
#include "string.h"

Button::Button(HWND parent, char *text):Widget(parent)
{
    activable =1;
    
    name="Button";
    state =0;
    this->text =text;
    
    setFont(font_STANDARD);
    rect.y0=0; rect.y1= font->height +2;
    rect.x0=0; rect.x1= 3+ strlen(text)* (font->width+1);
    
}

Button::~Button()
{
}

void Button::draw(Rect *r)
{
    if(isActive())
    {
        if(state)
        {
            pDC->fillRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x00);
            pDC->drawRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x0F);
            pDC->drawString(rect.x0+1, rect.y0+1, rect.x1-1,rect.y1-1,text, 0x0A,0x00, font);    
        }
        else
        {
            pDC->fillRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x0F);
            pDC->drawRect(rect.x0,rect.y0,  rect.x1,rect.y1, 0x0F);
            pDC->drawString(rect.x0+1, rect.y0+1, rect.x1-1,rect.y1-1,text, 0x00,0x0F, font);    
        }
    }
    else
    {
        pDC->fillRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x00);
        pDC->drawRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x0F);
        pDC->drawString(rect.x0+1, rect.y0+1, rect.x1-1,rect.y1-1,text, 0x0F,0x00, font);    
    }

    pDC->drawPoint(rect.x0, rect.y0, 0x00);
    pDC->drawPoint(rect.x0, rect.y1-1, 0x00);
    pDC->drawPoint(rect.x1-1, rect.y0, 0x00);
    pDC->drawPoint(rect.x1-1, rect.y1-1, 0x00);

    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);

}

void Button::setText(char *text)
{
    this->text =text;
    rect.x1 =rect.x0 + 3 + strlen(text)*(font->width+1);

    draw(&rect);
}

void Button::setFont(FONT *font)
{
    this->font =font;
    rect.x1 =rect.x0 + 3 + strlen(text)*(font->width+1);
    rect.y1 =rect.y0 + font->height +2;
}

void Button::setState(bool state)
{
    this->state =state;
}

bool Button::getState()
{
    return state;
}


int Button::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{
    switch(iMsg)
    {
    case MSG_KEY_DOWN:
        switch(wParam)
        {
        case KEY_ENTER:
            if(state ==0)
            {
                state =1;
                draw(&rect);
            
                //emit BUTTON DOWN
                signal[SIGNAL_BUTTON_DOWN].emitSignal();
            }
            break;
        }
        break;
        
    case MSG_KEY_UP:
        switch(wParam)
        {
        case KEY_ENTER:
            if(state ==1)
            {
                state =0;
                draw(&rect);
            
                //emit BUTTON UP
                signal[SIGNAL_BUTTON_UP].emitSignal();
            }
            break;
        }
        break;

    }


    return Widget::eventHandler(hWnd, iMsg, wParam, lParam);
}


